package cn.itcast.myapplication.room;

import android.content.Context;
import android.os.AsyncTask;

import androidx.lifecycle.LiveData;

import java.util.List;

 /**
  *
  *  配置信息 WordViewModel去调用里面的信息，因为有些数据不能在主线程中完成，所以要在这里声明
  *
  * */
//WordRepository（存放配置信息，因为有些数据不能在主线程中完成，所以要在这里声明和调用）

public class WordRepository {

    private WordDao wordDao;//数据库的增删改查都要在这里声明

    private WordDatabase wordDatabase;//创建数据库获取数据库增删改查

    private LiveData<List<Word>> allWordsLive;//获取数据列表

    public WordRepository(Context context){

        wordDatabase=WordDatabase.getINSTANCE(context);//单例模式 防止多次创建对象

        wordDao= wordDatabase.getWordDao();//获取到数据库

        allWordsLive= wordDao.getAllWords();//获取集合数据

      }

    //列表数据
    //获取数据列表
    public LiveData<List<Word>> getAllWordsLive() {
        return allWordsLive;

    }

    //添加
    public void insertWord(Word...words){

         new InsertAsyncTask(wordDao).execute(words);

     }

    //修改
    public void updateWord(Word...words){

        new UpdateAsyncTask(wordDao).execute(words);

    }

    //删除
    public void deleteWord(Word...words){

        new DeleteAsyncTask(wordDao).execute(words);

    }

    //删除全部
    public void DeleteAllWord(){

        new DeleteAllAsyncTask(wordDao).execute();

    }

    //获取搜索后的结果
    public LiveData<List<Word>> findWordsWithPatten(String patten){

        return wordDao.findWordsWithPatten(patten);

    }

    //因为增删改查的操作不能在主线程中完成，所有要在继承AsyncTask在AsyncTask中去实现
    static class InsertAsyncTask extends AsyncTask<Word,Void,Void> {

        WordDao wordDao;

        public InsertAsyncTask(WordDao wordDao){

            this.wordDao=wordDao;

        }

        @Override
        protected Void doInBackground(Word... words) {

            wordDao.insertWords(words);

            return null;

        }

    }


    // 因为增删改查的操作不能在主线程中完成，所有要在继承AsyncTask在AsyncTask中去实现
    static class UpdateAsyncTask extends AsyncTask<Word,Void,Void>{

        WordDao wordDao;

        public UpdateAsyncTask(WordDao wordDao){

            this.wordDao=wordDao;

        }


        @Override
        protected Void doInBackground(Word... words) {

            wordDao.updateWords(words);

            return null;

        }

    }

    // 因为增删改查的操作不能在主线程中完成，所有要在继承AsyncTask在AsyncTask中去实现
    static class DeleteAsyncTask extends AsyncTask<Word,Void,Void>{

        WordDao wordDao;

        public DeleteAsyncTask(WordDao wordDao){

            this.wordDao=wordDao;

        }

        @Override
        protected Void doInBackground(Word... words) {

            wordDao.deleteWords(words);

            return null;

        }

    }


    // 因为增删改查的操作不能在主线程中完成，所有要在继承AsyncTask在AsyncTask中去实现
    static class DeleteAllAsyncTask extends AsyncTask<Word,Void,Void>{

        WordDao wordDao;

        public DeleteAllAsyncTask(WordDao wordDao){

            this.wordDao=wordDao;

        }

        @Override
        protected Void doInBackground(Word... words) {

            wordDao.deleteAllWords();

            return null;

        }

    }

}
